﻿using MeetAI.Model;
using MeetAI.Model.DTO;
using MeetAI.Model.VO;
using MeetAI.Repository.IRepository;
using MeetAI.Service.IService;
using MeetAI.Utility;
using MeetAI.Utility.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MeetAI.Service
{
    public class Dy_Article_Service : BaseService<dy_article>, IDy_Article_Service
    {
        private readonly IDy_Article_Repository _repository;
        private readonly IDy_ClassType_Repository _class_repository;
        public Dy_Article_Service(IDy_Article_Repository repository, IDy_ClassType_Repository class_repository) : base(repository)
        {
            _repository = repository;
            _class_repository = class_repository;
        }
        /// <summary>
        /// 获取首页
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<IEnumerable<dy_article_index_vo>> getIndexArticleAsync(dy_article_dto dto)
        {
            var where = PredicateBuilder.True<dy_article>();
            if (dto.pid.HasValue)
            {
                where = where.And(s => s.tid.Equals(dto.pid));
            }
            var list = await _repository.FindPagedListAsync(where, dto.order, dto.page.Value, dto.size.Value);
            return list.Select(s => new dy_article_index_vo
            {
                title = s.title,
                litpic = string.Format("{0}{1}", staticDomain, s.litpic),
                time = s.addtime
            });
        }

        public async Task<PagedList<dy_article_index_vo>> getArticleAsync(dy_article_dto dto)
        {
            var where = PredicateBuilder.True<dy_article>();
            if (dto.pid.HasValue)
            {
                var classTypeIds = _class_repository.FindListByClause(s => s.pid.Equals(dto.pid)).Select(s => s.tid);
                if (classTypeIds != null && classTypeIds.Count() > 0)
                    where = where.And(s => classTypeIds.Contains(s.tid));
                else
                    where = where.And(s => s.tid.Equals(dto.pid));
            }
            where = where.And(s => s.isshow == 1);
            dto.order = "orders desc";
            var list = await _repository.getArticlePageList(where, dto.order, dto.page.Value, dto.size.Value);
            return new PagedList<dy_article_index_vo>(list.Select(s => new dy_article_index_vo
            {
                title = s.title,
                litpic =  string.Format("{0}{1}", staticDomain, s.litpic),
                time = s.time,
                videoUrl = !string.IsNullOrWhiteSpace(s.videoUrl) && s.videoUrl.StartsWith("http") ? s.videoUrl : string.Format("{0}{1}", staticDomain, s.videoUrl)
            }), dto.page.Value, dto.size.Value, list.TotalCount);
        }
    }
}
